﻿using BasicData.Domain;
using BasicData.ErrorCode;
using BasicData.Infastruucture;
using BasicData.Read.API.application.Command.Permission;
using MediatR;

namespace BasicData.Read.API.application.CommandHandler.Permission
{
    public class FindPermissionCommandHandler : IRequestHandler<FindPermissionCommand, ApiResult<List<int>>>
    {
        private readonly IBaseRepository<RolePermissionModel> roleperReo;

        public FindPermissionCommandHandler(IBaseRepository<RolePermissionModel> roleperReo)
        {
            this.roleperReo = roleperReo;
        }

        public Task<ApiResult<List<int>>> Handle(FindPermissionCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<int>> res = new ApiResult<List<int>>();
            var list = roleperReo.GetAll().Where(o => o.RoleId == request.RoleId).Select(o => o.PermissionId);
            res.Code = BaseEnums.Success;
            res.Msg = "角色权限反填成功";
            res.Data = list.ToList();


            return Task.FromResult(res);
        }
    }
}
